package com.dsj.gxrd.modules.people.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsj.gxrd.modules.people.domain.dto.RdUserInfoCockpit;
import com.dsj.gxrd.modules.people.domain.entity.RdUserInfo;
import com.dsj.gxrd.modules.people.domain.query.RdUserInfoQuery;
import com.dsj.gxrd.modules.people.domain.vo.*;
import com.dsj.gxrd.modules.people.mapper.RdUserInfoMapper;
import com.dsj.gxrd.modules.people.service.RdUserInfoService;
import com.ruoyi.common.core.utils.PageInfoUtil;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author songpin566
 * @date 2023/09/21
 **/
@Service
public class RdUserInfoServiceImpl extends ServiceImpl<RdUserInfoMapper, RdUserInfo> implements RdUserInfoService {


    @Override
//    @DataScope(deptAlias = "d", userAlias = "u")
    public PageInfoUtil<RdUserInfoVo> list(RdUserInfoQuery userInfoQuery) {
        //PageInfoUtil解决order by和 limit同时使用出现第一页第二页可能出现相同数据的问题
        List<RdUserInfoVo> list = this.baseMapper.selectListByQuery(userInfoQuery);
        PageInfoUtil<RdUserInfoVo> pageInfo=new PageInfoUtil<>(list);
        pageInfo.startPage(userInfoQuery.getPageNum(),userInfoQuery.getPageSize());
        return pageInfo;
    }

    @Override
    public RdUserInfoVo selectUserById(Long userId) {
        return this.baseMapper.selectUserById(userId);
    }

    @Override
    public List<String> getUserInfoNamesByIds(Collection<Long> userIds) {
        List<RdUserInfo> list = this.lambdaQuery().select(RdUserInfo::getName).in(RdUserInfo::getUserId,userIds).list();
        if(CollectionUtils.isNotEmpty(list)){
            return list.stream().map(RdUserInfo::getName).collect(Collectors.toList());
        }
        return new ArrayList<>(0);
    }

    @Override
    public List<RdUserInfoCockpit> cockpit() {
        return this.baseMapper.cockpit();
    }

    @Override
    public RdUserInfoRepresentVo representative() {
        List<RdUserInfoRepresent>sexCount=this.baseMapper.sexCount();
        List<RdUserInfoRepresent>partisanNameCount=this.baseMapper.partisanNameCount();
        List<RdUserInfoRepresent>nationalityCount=this.baseMapper.nationalityCount();
        List<RdUserInfoRepresent>levelCount=this.baseMapper.levelCount();
        return new RdUserInfoRepresentVo(sexCount,partisanNameCount,nationalityCount,levelCount);
    }

    @Override
    public RdUserInfoStatisticsVo middleRepresentative(Long deptId) {
        return this.baseMapper.middleRepresentative(deptId);
    }

    @Override
    public Map<String, List<CwhRepresent>> cwhRepresent() {
        List<CwhRepresent> cwhRepresents = this.baseMapper.cwhRepresent();
        return cwhRepresents.
                stream().
                collect(Collectors.
                        groupingBy(
                               item->item.getDeptName().length()<3?item.getDeptName(): StringUtils.substring(item.getDeptName(),0,3),
                                LinkedHashMap::new,
                                Collectors.toList()
                        ));
    }

    @Override
    public List<RdUserInfo> getUserInfoByIds(Set<Long> userIds) {
        return this.baseMapper.selectBatchIds(userIds);
    }

}
